#!/bin/bash

# Set title (if supported by terminal)
echo -e "\033]0;Deploy to Linux Server\007"

# Save current directory
CURRENT_DIR=$(pwd)

# Get user input
echo -n "Enter server IP address: "
read HOST_IP

echo -n "Enter SSH port (default 22): "
read PORT
PORT=${PORT:-22}

echo -n "Enter username: "
read USERNAME

echo -n "Enter remote path (e.g. /home/username): "
read REMOTE_PATH

# Confirm information
echo
echo "Please confirm the following information:"
echo "Server IP: $HOST_IP"
echo "Port: $PORT"
echo "Username: $USERNAME"
echo "Remote Path: $REMOTE_PATH"
echo
echo -n "Is this information correct? (Y/N): "
read CONFIRM

if [[ ! $CONFIRM =~ ^[Yy]$ ]]; then
    echo "Operation cancelled"
    exit 1
fi

# Create temp directory and copy files
echo
echo "Preparing files..."
TEMP_DIR="/tmp/deploy_temp"
rm -rf "$TEMP_DIR"
mkdir -p "$TEMP_DIR"

# Copy files excluding .pyc and cache
rsync -a --exclude '*.pyc' \
       --exclude '*.pyo' \
       --exclude '*.pyd' \
       --exclude '__pycache__' \
       "../main/" "$TEMP_DIR/main/"

# Use scp command to upload files
echo
echo "Uploading files..."
scp -P "$PORT" -r "$TEMP_DIR/main" "$USERNAME@$HOST_IP:$REMOTE_PATH"

# Check upload result
if [ $? -eq 0 ]; then
    echo
    echo "Deployment successful!"
else
    echo
    echo "Deployment failed. Please check your network connection and authentication."
fi

# Clean up temp files
rm -rf "$TEMP_DIR"

# Wait for user confirmation
echo
read -p "Press Enter to exit..." 